<!DOCTYPE html>
<head>
<!-- Following CSP directive allows this URL to prefetch itself. -->
<meta
  http-equiv="Content-Security-Policy"
  content="prefetch-src http://{{host}}:{{ports[http][0]}}/wpt_internal/prerender/resources/csp-prefetch-src-allow.sub.html">
</head>
<script src="/common/utils.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="utils.js"></script>
<script>

const params = new URLSearchParams(location.search);

// Take a key used for storing a test result in the server.
const key = params.get('key');

// The main test page (restriction-csp-prefetch-src.html in the parent
// directory) will load this page only with the "key" parameter. This page will
// then try prerendering itself with the "run-test" parameter. When "run-test"
// is in the URL we'll actually start the test process and record the results to
// send back to the main test page. We do this because the main test page cannot
// navigate itself but it also cannot open a popup to a prerendered browsing
// context so the prerender triggering and activation must both happen in this
// popup.
const run_test = params.has('run-test');
if (!run_test) {
  // Generate a new stash key so we can communicate with the prerendered page
  // about when to close the popup.
  const done_key = token();
  const url = new URL(document.URL);
  url.searchParams.append('run-test', '');
  url.searchParams.append('done-key', done_key);
  startPrerendering(url.toString());

  // Wait until the prerendered page signals us it's ready to close.
  nextValueFromServer(done_key).then(() => {
    window.close();
  });
} else {
  // Tell the harness the initial document.prerendering value.
  writeValueToServer(key, document.prerendering);

  // Tell the prerendering initiating page test being finished.
  const done_key = params.get('done-key');
  writeValueToServer(done_key, "done");
}

</script>
